package com.woniu113.junk.user.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu113.junk.commons.model.Result;

import com.woniu113.junk.commons.util.RedisUtil;
import com.woniu113.junk.commons.util.TokenUtil;
import com.woniu113.junk.user.Param.addressInfo.DefaultAddressParam;
import com.woniu113.junk.user.Param.addressInfo.ModifyAddressParam;
import com.woniu113.junk.user.Param.addressInfo.UserAddressInfoParam;
import com.woniu113.junk.user.controller.form.addressInfo.ModifyAddressForm;
import com.woniu113.junk.user.controller.form.addressInfo.UserAddressInfoForm;
import com.woniu113.junk.user.dto.address.FindAddressDTO;
import com.woniu113.junk.user.exception.UserException;
import com.woniu113.junk.user.model.AddressInfo;
import com.woniu113.junk.user.service.AddressInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@RestController
@RequestMapping("/addressInfo")
@Api("用户地址相关")
public class AddressInfoController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private AddressInfoService addressInfoService;
    @PostMapping("/userAddressInfo")
    @ApiOperation("用户添加地址")
    private Result userAddressInfo(@RequestBody UserAddressInfoForm form,@RequestHeader String token) throws Exception {
        UserAddressInfoParam param = BeanUtil.toBean(form, UserAddressInfoParam.class);
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        param.setUserId(userId);
        addressInfoService.userAddressInfo(param);
        return Result.getResult().setMsg("添加地址成功").setCode(200);
    }
    @GetMapping("/defaultAddress")
    @ApiOperation("设置默认地址")
    private Result defaultAddress(@RequestParam Integer addressId,@RequestHeader String token) throws Exception {
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        DefaultAddressParam param = new DefaultAddressParam();
        param.setAddressId(addressId);
        param.setUserId(userId);
        addressInfoService.defaultAddress(param);
        return Result.getResult().setMsg("设置默认地址成功").setCode(200);
    }
    @GetMapping("/deleteAddress")
    @ApiOperation("删除地址")
    private Result deleteAddress(@RequestParam Integer addressId,@RequestHeader String token) throws Exception {
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        DefaultAddressParam param = new DefaultAddressParam();
        param.setAddressId(addressId);
        param.setUserId(userId);
        addressInfoService.deleteAddress(param);
        return Result.getResult().setMsg("删除地址成功").setCode(200);
    }
    @GetMapping("/defaultAddressById")
    @ApiOperation("根据用户查询自己默认地址")
    private Result<AddressInfo> defaultAddressById(@RequestHeader String token) throws Exception {
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        AddressInfo addressInfo =addressInfoService.defaultAddressById(userId);
        return Result.getResult(AddressInfo.class).setData(addressInfo).setMsg("查询默认地址成功").setCode(200);
    }
//    @GetMapping("/addressByUserId")
//    @ApiOperation("根据用户查询自己地址")
//    private Result<List<AddressInfo>> addressByUserId(@RequestHeader String token) throws Exception {
//        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
//        List<AddressInfo> addressInfos =addressInfoService.addressByUserId(userId);
//        return Result.getListResult(AddressInfo.class).setData(addressInfos).setMsg("查询地址成功").setCode(200);
//    }

    @GetMapping("/addressByUserId")
    @ApiOperation("根据用户查询自己地址")
    private Result<List<AddressInfo>> addressByUserId(@RequestHeader String token,@RequestParam int pageNum,@RequestParam int pageSize) throws Exception {
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        // 计算查询的起始索引
        int offset = (pageNum - 1) * pageSize;
        FindAddressDTO dto = addressInfoService.addressByUserId(userId, offset, pageSize);
        int total = dto.getTotal();
        return Result.getListResult(AddressInfo.class).setData(dto.getList()).setMsg("查询地址成功").setCode(200).setTotal(total);
    }

    @GetMapping("/addressById")
    @ApiOperation("根据地址id查询地址")
    public Result<AddressInfo> addressById(@RequestParam("addressId") Integer addressId){
        AddressInfo addressInfo =addressInfoService.getById(addressId);
        if (addressInfo==null)throw new UserException("该地址不存在",2002);
        if (addressInfo.getAddressState()!=0)throw new UserException("该地址已删除",2003);
        return Result.getResult(AddressInfo.class).setData(addressInfo).setMsg("查询地址成功").setCode(200);
    }
    @GetMapping("/modifyAddress")
    @ApiOperation("修改地址")
    private Result modifyAddress(@RequestBody ModifyAddressForm form,@RequestHeader String token) throws Exception {
        ModifyAddressParam param = BeanUtil.toBean(form, ModifyAddressParam.class);
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        if (!form.getUserId().equals(userId))throw new UserException("非法操作",2004);
        addressInfoService.modifyAddress(param);
        return Result.getResult().setMsg("修改地址成功").setCode(200);
    }
}

